package com.example.server.controller;

import com.example.server.pojo.MyUser;
import com.example.server.pojo.RespBean;
import com.example.server.service.MyUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class MyUserCtl {
    @Autowired
    private MyUserService myUserService;

    @ApiOperation(value = "查询所有用户")
    @GetMapping("/findall")
    public RespBean findAll(){
        List<MyUser> result = myUserService.findAll();
        log.info("-----结果："+result);

        if (result != null){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(400,"fail",null);
        }
    }

    @ApiOperation(value = "查询所有用户")
    @GetMapping("/finduser/{id}")
    public RespBean findUser(@PathVariable("id") Long id){
        MyUser myuser = myUserService.findUser(id);
        log.info("-----结果："+myuser);

        if (myuser != null){
            return new RespBean(200,"success",myuser);
        }else {
            return new RespBean(400,"fail",null);
        }
    }

    @ApiOperation(value = "插入用户")
    @PostMapping("/adduser")
    public RespBean addUser(MyUser myUser){
        int result = myUserService.addUser(myUser);
        log.info("-----结果："+result);

        if (result > 0 ){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(400,"fail",null);
        }
    }

}
